﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace InventoryManagement
{
    public partial class InventoryForm : Form
    {
        string Url = string.Empty;

        public InventoryForm()
        {
            Url = ConfigurationSettings.AppSettings["url"];
            InitializeComponent();
        }

        private void InventoryForm_Load(object sender, EventArgs e)
        {
            GetStocks();
        }

        private void RefreshButton_Click(object sender, EventArgs e)
        {
            GetStocks();
        }

        private void InventoryGridView_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (InventoryGridView.SelectedRows.Count != 1)
            {
                MessageBox.Show("Select one stock to update quantity");
            }
            else
            {
                AddOrModifyForm newStock = new AddOrModifyForm();
                newStock.stock = InventoryGridView.SelectedRows[0].Cells[0].Value.ToString();
                newStock.formula = InventoryGridView.SelectedRows[0].Cells[2].Value.ToString();
                newStock.quantity = InventoryGridView.SelectedRows[0].Cells[3].Value.ToString();
                newStock.PopulateValues();
                newStock.EditOnlyQuantity();

                newStock.ShowDialog();

                if (newStock.Cancelled == false)
                {
                    string json = JsonConvert.SerializeObject(new { newStock.stock, newStock.quantity });
                    HttpHelper.Put(Url+"Stock", json);
                    GetStocks();
                }
            }
        }

        private void InventoryGridView_MouseClick(object sender, MouseEventArgs e)
        {

        }

        private void GetStocks()
        {
            try
            {
                string getStocksResponse = HttpHelper.Get(Url + "Stock");
                GetStockResponse response = (GetStockResponse)JsonConvert.DeserializeObject<GetStockResponse>(getStocksResponse);

                InventoryGridView.Rows.Clear();

                foreach (var stock in response.Stocks)
                {
                    InventoryGridView.Rows.Add(stock.Name, stock.Code, stock.Formula, stock.Quantity);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Problem in connecting to server, please check your network settings");
            }
        }

        private void AddButton_Click(object sender, EventArgs e)
        {
            AddOrModifyForm newStock = new AddOrModifyForm();
            newStock.ShowDialog();

            if (newStock.Cancelled == false)
            {
                string json = JsonConvert.SerializeObject(new {newStock.stock, newStock.formula, newStock.quantity});
                HttpHelper.Post(Url + "Stock", json);
                GetStocks();
            }
        }

        private void ModifyButton_Click(object sender, EventArgs e)
        {
            if (InventoryGridView.SelectedRows.Count != 1)
            {
                MessageBox.Show("Select one stock to modify quantity");
            }
            else
            {
                AddOrModifyForm newStock = new AddOrModifyForm();
                newStock.stock = InventoryGridView.SelectedRows[0].Cells[0].Value.ToString();
                newStock.formula = InventoryGridView.SelectedRows[0].Cells[2].Value.ToString();
                newStock.quantity = InventoryGridView.SelectedRows[0].Cells[3].Value.ToString();
                newStock.PopulateValues();

                newStock.ShowDialog();

                if (newStock.Cancelled == false)
                {
                    string json = JsonConvert.SerializeObject(new { newStock.stock, newStock.formula, newStock.quantity });
                    HttpHelper.Post(Url + "Stock", json);
                    GetStocks();
                }
            }
        }
    }
}
